package com.yitiao.multithread.threadlocal;

import cn.hutool.core.thread.ThreadUtil;
import com.alibaba.ttl.TransmittableThreadLocal;
import com.alibaba.ttl.threadpool.TtlExecutors;

import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TTLSimple {
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(1);
        // 需配合 ttl 的线程池也可以做到线程复用
        Executor ttlExecutor = TtlExecutors.getTtlExecutor(executorService);
        TransmittableThreadLocal<Object> transmittableThreadLocal = new TransmittableThreadLocal<>();
        for (int i = 0; i < 10; i++) {
            transmittableThreadLocal.set(i);
            ThreadUtil.execute(()-> System.out.println(transmittableThreadLocal.get()));
        }
    }
}
